
package com.ikamobile.flickrer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class PhotoAdapter extends BaseAdapter {

    protected static final String LOG_TAG = PhotoAdapter.class.getSimpleName();
    private ArrayList<Photo> mPhotos = new ArrayList<Photo>();
    private LayoutInflater mInflater;
    private PhotoHelper mPhotoHelper;

    public PhotoAdapter(Context context, List<Photo> photos) {
        mInflater = LayoutInflater.from(context);
        mPhotos.addAll(photos);
        mPhotoHelper = new PhotoHelper(context);
    }

    public int getCount() {
        return mPhotos.size();
    }

    public Object getItem(int position) {
        return mPhotos.get(position);
    }

    public long getItemId(int position) {
        return mPhotos.get(position).hashCode();
    }

    class ViewHolder {
        ImageView photo;
        TextView title;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ViewHolder vh;
        if (view == null) {
            view = mInflater.inflate(R.layout.photo_item, null);
            vh = new ViewHolder();
            vh.photo = (ImageView) view.findViewById(R.id.photo);
            vh.title = (TextView) view.findViewById(R.id.title);
            view.setTag(vh);
        } else
            vh = (ViewHolder) view.getTag();
        Photo photo = (Photo) getItem(position);
        mPhotoHelper.loadOrSetImage(vh.photo, photo.getSmallSquareUrl(), null);
        vh.title.setText(photo.title);
        return view;
    }

}
